package com.example.exception_demo.handler;

import com.example.exception_demo.exception.InvalidDattaException;
import com.example.exception_demo.exception.UserNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResponseEntity<Map<String,String>> handlerUserNotFoundException(UserNotFoundException ex){
        Map<String,String> response=new HashMap<>();
        response.put("error","User Not Fonud");
        response.put("message",ex.getMessage());
        return new ResponseEntity<>(response,HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(InvalidDattaException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<Map<String,String>> handlerInvalidDataException(InvalidDattaException ex){
        Map<String,String> response=new HashMap<>();
        response.put("error","Invalid Data");
        response.put("message",ex.getMessage());
        return new ResponseEntity<>(response,HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<Map<String,String>> handlerGeneralException(Exception ex){
        Map<String,String> response=new HashMap<>();
        response.put("error","Internal Server Error");
        response.put("message",ex.getMessage());
        return new ResponseEntity<>(response,HttpStatus.BAD_REQUEST);
    }
}
